package com.simon.hse.activity.law;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.DropDownInput;
import com.simon.hse.http.entity.DropDownOutput;
import com.simon.hse.http.entity.FL_StandardInformationGetFileLevelListInput;
import com.simon.hse.http.entity.FL_StandardInformationGetFileLevelListOutput;
import com.simon.hse.http.entity.FL_StandardInformationGetListInput;
import com.simon.hse.http.entity.LawOutput;
import com.simon.hse.http.entity.SafetyCardGetListInput;
import com.simon.hse.http.entity.SafetyCardGetListOutput;
import com.simon.hse.http.entity.TrainPersonOutput;
import com.simon.hse.interfaces.PopListener;
import com.simon.hse.utils.DateTimeUtils;
import com.simon.hse.utils.PopupWindowUtils;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import retrofit2.Call;

/**
 * 法律法规管理
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_law_manage)
public class LawManageActivity extends BaseActivity implements PopListener {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.ll_law_tab)
    LinearLayout mLl_law_tab;

    @ViewInject(R.id.ll_function_1)
    LinearLayout mLl_function_1;

    @ViewInject(R.id.tv_function_1)
    TextView mTv_function_1;

    @ViewInject(R.id.tv_key_1)
    TextView tv_key_1;

    @ViewInject(R.id.ll_function_2)
    LinearLayout mLl_function_2;

    @ViewInject(R.id.tv_function_2)
    TextView mTv_function_2;

    @ViewInject(R.id.tv_key_2)
    TextView tv_key_2;

    @ViewInject(R.id.et_function_3)
    EditText et_function_3;

    @ViewInject(R.id.smartrefresh)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.recyclerview)
    RecyclerView mRecyclerView;

    private CommonEmptyAdapter mCommonEmptyAdapter;
    private int currentPage = 1, totalPage = 1;
    private List<LawOutput.DataBean.PageDataBean> mDataBeanList = new ArrayList<>();// 法律法规列表
    private List<DropDownOutput.DataBean.DataDicListBean.ValueBean> mFileLevelList = new ArrayList<>();// 获取文件等级列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTv_common_title.setText("法律法规管理");
        mTv_function_1.setText(MyApplication.getInstance().mFactoryList.get(0).getValue());
        String fac = MyApplication.getInstance().mFactoryList.get(0).getKey();
        tv_key_1.setText(fac);
        initRecyclerView();
        initData();
        initFileLevel();
    }

    @Event(value = {R.id.iv_common_left, R.id.ll_function_1, R.id.ll_function_2})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.ll_function_1:
                initPop(mLl_function_1, mTv_function_1, tv_key_1, MyApplication.getInstance().mFactoryList,this);
                break;
            case R.id.ll_function_2:
                initPop(mLl_function_2, mTv_function_2, tv_key_2, mFileLevelList,this);
                break;
        }
    }

    private void initRecyclerView() {
        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage * 10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(LawManageActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(LawManageActivity.this, R.layout.layout_item_law, mDataBeanList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<LawOutput.DataBean.PageDataBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, LawOutput.DataBean.PageDataBean dataBean) {
                TextView tv_law_msg = holder.getView(R.id.tv_law_msg);
                tv_law_msg.setText(dataBean.getLawRegulationsName());
                TextView tvLawLevel = holder.getView(R.id.tv_law_level);
                tvLawLevel.setText(dataBean.getFileLeve());
                TextView tvLawTime = holder.getView(R.id.tv_law_time);
                tvLawTime.setText(dataBean.getLastImplementationDate());
            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {
                initData();
            }

            @Override
            public void setOnItemClickListener(View view, int position) {
                Intent intent = new Intent(LawManageActivity.this, LawDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("ID", mDataBeanList.get(position).getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mCommonEmptyAdapter.setHasStableIds(true);
        mRecyclerView.setAdapter(mCommonEmptyAdapter);

        et_function_3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mDataBeanList.clear();
                initData();
            }
        });
    }

    /**
     * 法律法规列表
     */
    private void initData() {
        FL_StandardInformationGetListInput lawInput = new FL_StandardInformationGetListInput();
        FL_StandardInformationGetListInput.ModelBean modelBean = new FL_StandardInformationGetListInput.ModelBean();

        // 所属工厂
        String createFactory = tv_key_1.getText().toString();
        if (!TextUtils.isEmpty(createFactory)) {
            modelBean.setCreateFactory(tv_key_1.getText().toString());
        }
        // 文件级别
        String fileLeve = tv_key_2.getText().toString();
        if (!TextUtils.isEmpty(fileLeve)) {
            modelBean.setFileLeve(tv_key_2.getText().toString());
        }
        // 备注
        String remarks = et_function_3.getText().toString();
        if (!TextUtils.isEmpty(remarks)) {
            modelBean.setRemarks(remarks);
        }
        lawInput.setModel(modelBean);
        lawInput.setLimit(10);
        lawInput.setOffset(10 * (currentPage - 1));

        Call<String> call = HttpManager.getInstance().requestService().LAW_LIST(HttpManager.getInstance().requestBody(lawInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                LawOutput lawOutput = new Gson().fromJson(result, LawOutput.class);
                if (lawOutput.getResult().equals("Success")) {
                    totalPage = lawOutput.getData().getRowCount();
                    List<LawOutput.DataBean.PageDataBean> pageDataBeanList = lawOutput.getData().getPageData();
                    mDataBeanList.addAll(pageDataBeanList);
                    mCommonEmptyAdapter.notifyDataSetChanged();
                } else {
//                    ToastUtils.getInstance().showShortToastBottom(lawOutput.getMessage());
                }
                mSmartRefresh.finishRefresh();
                mSmartRefresh.finishLoadMore();
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
                mSmartRefresh.finishRefresh();
                mSmartRefresh.finishLoadMore();
            }
        });
    }

    /**
     * 文件等级
     */
    private void initFileLevel() {
        FL_StandardInformationGetFileLevelListInput input = new FL_StandardInformationGetFileLevelListInput();
        FL_StandardInformationGetFileLevelListInput.ModelBean modelBean = new FL_StandardInformationGetFileLevelListInput.ModelBean();
        input.setModel(modelBean);
        input.setOffset(0);
        input.setLimit(80);
        Call<String> call = HttpManager.getInstance().requestService().FL_StandardInformation_GetFileLevelList(HttpManager.getInstance().requestBody(input));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                FL_StandardInformationGetFileLevelListOutput output = new Gson().fromJson(result, FL_StandardInformationGetFileLevelListOutput.class);
                if (output.getResult().equals("Success")) {
                    List<FL_StandardInformationGetFileLevelListOutput.DataBean.PageDataBean> pageDataBeanList = output.getData().getPageData();
                    DropDownOutput.DataBean.DataDicListBean.ValueBean valueBean = new DropDownOutput.DataBean.DataDicListBean.ValueBean();
                    valueBean.setKey("");
                    valueBean.setValue("全部");
                    mFileLevelList.add(valueBean);
                    for (int i = 0; i < pageDataBeanList.size(); i++) {
                        DropDownOutput.DataBean.DataDicListBean.ValueBean bean = new DropDownOutput.DataBean.DataDicListBean.ValueBean();
                        bean.setValue(pageDataBeanList.get(i).getFileLevelName());
                        bean.setKey(pageDataBeanList.get(i).getId());
                        mFileLevelList.add(bean);
                    }
                } else {
                    ToastUtils.getInstance().showShortToastBottom(output.getMessage().toString());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    @Override
    public void upData() {
        currentPage = 1;
        mDataBeanList.clear();
        initData();
    }
}

